package com.communal.web.form.cr;

import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONObject;

import com.communal.model.cr.CommunalRecord;
import com.communal.util.ApplicationUtils;
import com.communal.web.form.BaseForm;

/**
 * Communal record form with common fields for communal records <br>
 * @author Created by andrey.kuznetsov <br>
 * @author Last modified by $Author$ <br>
 * @author Last modified on $Date$ at revision $Revision$ <br>
 */
public class CRForm extends BaseForm{

	public static final String YEAR = "year";
	public static final String MONTH = "month";
	public static final String SUMMA = "summa";
	public static final String DATE = "date";
	public static final String MONTH_YEAR = "monthYear";
	
	protected Short year;
	protected Short month;
	protected Double summa;
	protected String date;
	protected String monthYear;
	
	/**
	 * Constructor from JSON data for {@link CRForm} <br>
	 * @param p_data - json data; {@link Object} <br>
	 * @param p_constructorFlag - flag for constructor can be true or false
	 */
	public CRForm(Object p_data, boolean p_constructorFlag){
		super(p_data, true);
		JSONObject jsonObject = JSONObject.fromObject(p_data);
		MorphDynaBean bean = (MorphDynaBean) JSONObject.toBean(jsonObject);
		year = Short.parseShort(bean.get(YEAR).toString());
		month = Short.parseShort(bean.get(MONTH).toString());
		summa = Double.parseDouble(bean.get(SUMMA).toString());
		date = bean.get(DATE).toString();
	}
	
	/**
	 * Constructor for {@link MoneyForm} from money model <br>
	 * @param p_money - money pojo; {@link IMoney}
	 */
	public CRForm(CommunalRecord p_communalRecord, String p_monthYear){
		super( p_communalRecord );
		year = p_communalRecord.getYear();
		month = p_communalRecord.getMonth();
		summa = p_communalRecord.getSumma() != null ? p_communalRecord.getSumma() : 0d;
		date = p_communalRecord.getDate() != null ? 
			ApplicationUtils.getDateISOFromLong(p_communalRecord.getDate()) : 
			ApplicationUtils.getDateISOFromLong(System.currentTimeMillis());
		monthYear = p_monthYear;
	}
	
	/**
	 * Returns year of record <br>
	 * @return {@link Short};
	 */
	public Short getYear() {
		return year;
	}
	
	/**
	 * Sets year for record <br>
	 * @param p_year - {@link Short};
	 */
	public void setYear(Short p_year) {
		year = p_year;
	}
	
	/**
	 * Returns month of record <br>
	 * @return {@link Short};
	 */
	public Short getMonth() {
		return month;
	}
	
	/**
	 * Sets month for record <br>
	 * @param p_month - {@link Short};
	 */
	public void setMonth(Short p_month) {
		month = p_month;
	}
	
	/**
	 * Returns summa of record <br>
	 * @return {@link Double};
	 */
	public Double getSumma() {
		return summa;
	}
	
	/**
	 * Sets summa for record <br>
	 * @param p_summa - {@link Double};
	 */
	public void setSumma(Double p_summa) {
		summa = p_summa;
	}
	
	/**
	 * Returns date of record <br>
	 * @return {@link String};
	 */
	public String getDate() {
		return date;
	}
	
	/**
	 * Sets date of record <br>
	 * @param p_date - {@link String};
	 */
	public void setDate(String p_date) {
		date = p_date;
	}

	/**
	 * Returns month year string (for displaying in chart for exapmle jan-2012)
	 * @return {@link String};
	 */
	public String getMonthYear() {
		return monthYear;
	}

	/**
	 * Sets month year string (for displaying in chart) 
	 * @param p_monthYear - {@link String};
	 */
	public void setMonthYear(String p_monthYear) {
		monthYear = p_monthYear;
	}
	
	
}
